
# Installation directories.
LIBDIR ?= ../../../bin
SHLIBDIR ?= ../../../bin
INCLUDEDIR ?= ../../include

LIBBASE ?= $(shell basename $(LIBDIR))

VERSION = $(shell cat ../VERSION)
LIBVERSION = 1

LIBA=libselinux.a 
TARGET=libselinux.so
LIBSO=$(TARGET).$(LIBVERSION)

LDFLAGS=-Wl,-rpath=.:./bin

ifeq ($(DISABLE_AVC),y)
	UNUSED_SRCS+=avc.c avc_internal.c avc_sidtab.c mapping.c stringrep.c checkAccess.c
endif
ifeq ($(DISABLE_BOOL),y)
	UNUSED_SRCS+=booleans.c
endif

SRCS= $(filter-out $(UNUSED_SRCS) audit2why.c, $(wildcard *.c))

MAX_STACK_SIZE=32768

OBJS= $(patsubst %.c,%.o,$(SRCS))
LOBJS= $(patsubst %.c,%.lo,$(SRCS))
CFLAGS ?= -O -Wall -W -Wundef -Wformat-y2k -Wformat-security -Winit-self -Wmissing-include-dirs \
          -Wunused -Wunknown-pragmas -Wstrict-aliasing -Wshadow -Wpointer-arith \
          -Wbad-function-cast -Wcast-align -Wwrite-strings -Wlogical-op -Waggregate-return \
          -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes \
          -Wmissing-declarations -Wmissing-noreturn -Wmissing-format-attribute \
          -Wredundant-decls -Wnested-externs -Winline -Winvalid-pch -Wvolatile-register-var \
          -Wdisabled-optimization -Wbuiltin-macro-redefined -Wpacked-bitfield-compat \
          -Wsync-nand -Wattributes -Wcoverage-mismatch -Wmultichar -Wcpp \
          -Wdeprecated-declarations -Wdiv-by-zero -Wdouble-promotion -Wendif-labels -Wextra \
          -Wformat-contains-nul -Wformat-extra-args -Wformat-zero-length -Wformat=2 -Wmultichar \
          -Wnormalized=nfc -Woverflow -Wpointer-to-int-cast -Wpragmas -Wsuggest-attribute=const \
          -Wsuggest-attribute=noreturn -Wsuggest-attribute=pure -Wtrampolines \
          -Wno-missing-field-initializers -Wno-sign-compare -Wjump-misses-init \
          -Wno-format-nonliteral -Wframe-larger-than=$(MAX_STACK_SIZE) -Wp,-D_FORTIFY_SOURCE=2 \
          -fstack-protector-all --param=ssp-buffer-size=4 -fexceptions \
          -fasynchronous-unwind-tables -fdiagnostics-show-option -funit-at-a-time \
          -fipa-pure-const -Wno-suggest-attribute=pure -Wno-suggest-attribute=const \
          -Werror -Wno-aggregate-return -Wno-redundant-decls

override CFLAGS += -I$(INCLUDEDIR) -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 $(EMFLAGS)

RANLIB ?= ranlib

ARCH := $(patsubst i%86,i386,$(shell uname -m))
ifneq (,$(filter i386,$(ARCH)))
TLSFLAGS += -mno-tls-direct-seg-refs
endif

ifneq ($(strip $(TOOLS_BUILD_SHARED)),)
all: $(LIBSO)
else
all: $(LIBA)
endif

$(LIBA): $(OBJS)
	$(AR) rcs $@ $^
	$(RANLIB) $@

$(LIBSO): $(LOBJS)
	$(CC) $(CFLAGS) -shared -o $@ $^ -lpcre -ldl $(LDFLAGS) -L$(LIBDIR) -Wl,-soname,$(LIBSO),-z,defs,-z,relro
	ln -sf $@ $(TARGET) 

%.o:  %.c policy.h
	$(CC) $(CFLAGS) $(TLSFLAGS) -c -o $@ $<

%.lo:  %.c policy.h
	$(CC) $(CFLAGS) -fPIC -DSHARED -c -o $@ $<

install: all 
	test -d $(LIBDIR) || install -m 755 -d $(LIBDIR)
ifneq ($(strip $(TOOLS_BUILD_SHARED)),)
	test -d $(SHLIBDIR) || install -m 755 -d $(SHLIBDIR)
	install -m 755 $(LIBSO) $(SHLIBDIR)
	ln -sf $(LIBSO) $(LIBDIR)/$(TARGET)
else
	test -d $(LIBDIR) || install -m 755 -d $(LIBDIR)
	install -m 755 $(LIBA) $(LIBDIR)
endif

clean: 
	-rm -f $(OBJS) $(LOBJS) $(LIBA) $(LIBSO) $(TARGET) *.o *.lo *~

.PHONY: all clean install
